package library.and.xps.com.step.view.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.google.gson.Gson;
import com.jude.utils.JUtils;
import com.orhanobut.logger.Logger;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.realm.Realm;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.adapter.AbstractRecyclerAdapter;
import library.and.xps.com.step.adapter.TeamActiveListAdapter;
import library.and.xps.com.step.m.model.TeamActiveList;
import library.and.xps.com.step.m.model.User;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.ListDividerItemDecoration;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.view.base.BaseSwipeRefreshFragment;

/**
 * Created by XPS on 2017/5/22.
 */

public class ActiveListFragment extends BaseSwipeRefreshFragment implements AbstractRecyclerAdapter.OnRecyclerViewItemClickListener {
    User user;
    int page;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setBackgroundColor(Color.parseColor("#F0F0F0"));
        recyclerView.addItemDecoration(new ListDividerItemDecoration((int) (1 * getActivity().getResources().getDisplayMetrics().density)));
        abstractRecyclerAdapter = new TeamActiveListAdapter(activity, recyclerView);
        abstractRecyclerAdapter.setOnItemClickListener(this);
        setRefreshEnable(false);
        super.onActivityCreated(savedInstanceState);
        Realm realm = Realm.getDefaultInstance();
        user = realm.where(User.class).findFirst();
        request(1);
    }

    @Override
    protected void getData(int pageIndex) {
        page = pageIndex+1;
        request(page);
    }

    void request(int index) {
        String data = URLConstants.TEAMACTIVE.TEAM_ACTIVITIES + "?" + "userid=" + user.getUserid() + "&teamid=" + user.getTeams().get(0).getTId() + "&page=" + index;
        OkHttpClientUtils.getDataAsync(getActivity(), data, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                if (HttpResponsHelper.SuccessResponce(response)) {
                    Logger.json(response.getBody());
                    TeamActiveList teamActiveList = new Gson().fromJson(response.getBody(), TeamActiveList.class);
                    adapterListData = teamActiveList.getList();
                    onResponce();
                }
            }

            @Override
            public void onStart() {
            }
        }, "");
    }

    @Override
    public void onItemClick(View view, int position) {
    }
}
